/*
 * SelectCustomer.java
 *
 * Created on February 22, 2007, 1:12 PM
 * @author Jeff Jenkins
 * @descritption: This method provides a user intereface to select a customer to view or update
 * the customer. Or allows you to create a customer. It also contains 
 * funtinality to create a new order for a customer or for the default customer
 */

package edu.byu.isys413.jjenkins.view;

import edu.byu.isys413.jjenkins.data.*;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author  Jeff
 *
 * @descritption: This method provides a user intereface to select a customer to view or update
 * the customer. Or allows you to create a customer. It also contains 
 * funtinality to create a new order for a customer or for the default customer
 */
public class SelectCustomer extends javax.swing.JFrame {
    //A list of all the customers
    List li;
    
    //Singleton variable
    private static SelectCustomer instance = null;
    
    /** Creates new form SelectCustomer
     *  I choose to implement the singleton pattern
     *  so if in the futer if any other windows call this window they will
     *  only be allowed to have one at a atime*/
    private SelectCustomer() {
        initComponents();
        this.pack();
        populateList();
        
        
    }
    
    /* implements singleton pattern to insure that only one instance of this 
     * gui is created*/
    
    public static synchronized SelectCustomer getInstance() {
        
        if (instance == null) {
            instance = new SelectCustomer();
        }
        
        return instance;
    }
    
    void populateList(){
        try{
            cmbCust.removeAllItems();
            li = CustomerDAO.getInstance().getAll();
            for(int i = 0; i<li.size(); i++){
                cmbCust.addItem(((String[])li.get(i))[2] + ", " + ((String[])li.get(i))[1]+ "                  " + ((String[])li.get(i))[3]);
            this.setVisible(true);
            }
        }catch(DataException ex){
            JOptionPane.showMessageDialog(null,ex.toString(),"Error populating List", JOptionPane.ERROR_MESSAGE);
            
        }
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel8 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        cmbCust = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnView = new javax.swing.JButton();
        btnNew = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.BorderLayout(15, 15));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Customer Information");
        jPanel8.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(420, 250));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel5.setPreferredSize(new java.awt.Dimension(200, 40));
        jPanel7.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel7.setPreferredSize(new java.awt.Dimension(300, 20));
        jLabel1.setText("Choose Customer to View or Edit:");
        jLabel1.setPreferredSize(new java.awt.Dimension(300, 14));
        jPanel7.add(jLabel1);

        jPanel5.add(jPanel7, java.awt.BorderLayout.WEST);

        jPanel1.add(jPanel5, java.awt.BorderLayout.NORTH);

        jPanel6.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jPanel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(15, 15, 15, 15));
        jPanel6.setPreferredSize(new java.awt.Dimension(100, 50));
        cmbCust.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<Choose or Type In Name>" }));
        cmbCust.setPreferredSize(new java.awt.Dimension(350, 20));
        jPanel6.add(cmbCust);

        jPanel1.add(jPanel6, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        getContentPane().add(jPanel8, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setPreferredSize(new java.awt.Dimension(170, 40));
        jPanel3.setPreferredSize(new java.awt.Dimension(0, 40));
        btnView.setText("View/Edit");
        btnView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewActionPerformed(evt);
            }
        });

        jPanel3.add(btnView);

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        jPanel3.add(btnNew);

        jPanel2.add(jPanel3, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel2, java.awt.BorderLayout.EAST);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        try {
            
            // Gets new Customer
            CustomerBO cust = CustomerDAO.getInstance().create();
            
            //Passes to the next Screen
            CustomerView.getInstance().loadCustomer(cust, true);
           
        } catch (DataException ex) {
            JOptionPane.showMessageDialog(null,ex.toString(),"Error creating customer", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_btnNewActionPerformed
    /* This method retrieves the customer object for a certain customer and passes it to the view
     * customer GUI. */
    
    private void btnViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewActionPerformed
        try {
            
            // Retrives Customer ID
            String id = ((String[])li.get((cmbCust.getSelectedIndex())))[0];
            
            //Gets Customer Object
            
            CustomerBO cust = CustomerDAO.getInstance().read(id);
            
            //Passes to the next Screen
            CustomerView.getInstance().loadCustomer(cust, false);
            
        } catch (DataException ex) {
            JOptionPane.showMessageDialog(null,ex.toString(),"Error retrieving customer", JOptionPane.ERROR_MESSAGE);
            
        }
    }//GEN-LAST:event_btnViewActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SelectCustomer().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnView;
    private javax.swing.JComboBox cmbCust;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    // End of variables declaration//GEN-END:variables
    
}
